#!/usr/bin/env bash

################################################################################
# Finds changed uncovered lines.
#
# Usage:
#     check/pytest-and-incremental-coverage [BASE_REVISION] [PYTEST_ARGS]
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a line is considered to have "changed"). To make
# the tool more consistent, it actually diffs against the most recent common
# ancestor of the specified id and HEAD. So if you choose 'origin/main' you're
# actually diffing against the output of 'git merge-base origin/main HEAD'.
#
# If you don't specify a base revision, the following defaults will be tried,
# in order, until one exists:
#
#     1. upstream/main
#     2. origin/main
#     3. main
#
# If none exists, the script fails.
################################################################################

# Get the working directory to the repo root.
thisdir=$(dirname "${BASH_SOURCE[0]:?}") || exit $?
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) || exit $?
cd "${repo_dir}" || exit $?

PYTEST_ARGS=()
ANALYZE_COV=1
BASEREV=""

if [[ "$1" == [^-]* ]]; then
    BASEREV="$1"
    shift
fi

for arg in "$@"; do
    if [[ "${arg}" == "--no-analyze" ]]; then
        ANALYZE_COV=0
    else
        PYTEST_ARGS+=("${arg}")
    fi
done

# Figure out which revision to compare against.
if [ -n "${BASEREV}" ]; then
    if ! git rev-parse --verify --quiet --no-revs "${BASEREV}^{commit}"; then
        echo -e "\033[31mNo revision '${BASEREV}'.\033[0m" >&2
        exit 1
    fi
    rev="${BASEREV}"
elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
    rev=upstream/main
elif [ "$(git cat-file -t origin/main 2> /dev/null)" == "commit" ]; then
    rev=origin/main
elif [ "$(git cat-file -t main 2> /dev/null)" == "commit" ]; then
    rev=main
else
    echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/main' or 'HEAD~1').\033[0m" >&2
    exit 1
fi
base=$(git merge-base "${rev}" HEAD)
if [ "$(git rev-parse "${rev}")" == "${base}" ]; then
    echo -e "Comparing against revision '${rev}'." >&2
else
    echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
    rev="${base}"
fi

source dev_tools/pypath || exit $?

# Run tests while producing coverage files.
check/pytest --cov "${PYTEST_ARGS[@]}"
pytest_result=$?


# assume successful cover_result in case coverage is not run
cover_result=0
if (( ANALYZE_COV )); then
    # Convert to .py,cover files.
    coverage annotate

    # Analyze coverage files.
    python dev_tools/check_incremental_coverage_annotations.py "${rev}"
    cover_result=$?

    # Clean up generated coverage files.
    git clean --force --quiet -x -- "*.py,cover"
fi

# Report result.
if (( pytest_result || cover_result )); then
    exit 1
fi
exit 0
